﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FollowUpNetwork.Models;
using PagedList;

namespace FollowUpNetwork.Controllers
{
    [Authorize(Roles = "Vip")]
    public class TabletLeaderboardsController : Controller
    {
        private FollowUpNetworkContext db = new FollowUpNetworkContext();

        // GET: TabletLeaderboards
        public ActionResult Index(string sortBy, string currentSort, int? page)
        {
            int pageIndex = 1;
            int pageSize = 20;
            pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
            ViewBag.CurrentSort = sortBy;
            sortBy = string.IsNullOrEmpty(sortBy) ? "Name" : sortBy;
            IPagedList<TabletLeaderboard> lstStudent = null;
            switch (sortBy)
            {
                case "TabletRanking":
                    //如果sortBy==currentSort，就按照对应字段降序排列，并分页。否则升序。
                    if (sortBy.Equals(currentSort))
                    {
                        lstStudent = db.Set<TabletLeaderboard>().
                         OrderByDescending(s => s.TabletRanking).
                         ToPagedList(pageIndex, pageSize);
                    }
                    else
                    {
                        lstStudent = db.Set<TabletLeaderboard>().
                         OrderBy(s => s.TabletRanking).
                         ToPagedList(pageIndex, pageSize);
                    }
                    break;
                case "TabletPrice":
                    //如果sortBy==currentSort，就按照对应字段降序排列，并分页。否则升序。
                    if (sortBy.Equals(currentSort))
                    {
                        lstStudent = db.Set<TabletLeaderboard>().
                         OrderByDescending(s => s.TabletPrice).
                         ToPagedList(pageIndex, pageSize);
                    }
                    else
                    {
                        lstStudent = db.Set<TabletLeaderboard>().
                         OrderBy(s => s.TabletPrice).
                         ToPagedList(pageIndex, pageSize);
                    }
                    break;

                default:
                    //如果sortBy==currentSort，就按照对应字段降序排列，并分页。否则升序。
                    if (sortBy.Equals(currentSort))
                    {
                        lstStudent = db.Set<TabletLeaderboard>().
                         OrderByDescending(s => s.TabletRanking).
                         ToPagedList(pageIndex, pageSize);
                    }
                    else
                    {
                        lstStudent = db.Set<TabletLeaderboard>().
                         OrderBy(s => s.TabletRanking).
                         ToPagedList(pageIndex, pageSize);
                    }

                    break;
            }
            return View(lstStudent);
        }

        // GET: TabletLeaderboards/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TabletLeaderboard tabletLeaderboard = db.TabletLeaderboards.Find(id);
            if (tabletLeaderboard == null)
            {
                return HttpNotFound();
            }
            return View(tabletLeaderboard);
        }

        // GET: TabletLeaderboards/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: TabletLeaderboards/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "TabletLeaderboardId,TabletRanking,TabletModel,TabletPrice")] TabletLeaderboard tabletLeaderboard)
        {
            if (ModelState.IsValid)
            {
                db.TabletLeaderboards.Add(tabletLeaderboard);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tabletLeaderboard);
        }

        // GET: TabletLeaderboards/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TabletLeaderboard tabletLeaderboard = db.TabletLeaderboards.Find(id);
            if (tabletLeaderboard == null)
            {
                return HttpNotFound();
            }
            return View(tabletLeaderboard);
        }

        // POST: TabletLeaderboards/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "TabletLeaderboardId,TabletRanking,TabletModel,TabletPrice")] TabletLeaderboard tabletLeaderboard)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tabletLeaderboard).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tabletLeaderboard);
        }

        // GET: TabletLeaderboards/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TabletLeaderboard tabletLeaderboard = db.TabletLeaderboards.Find(id);
            if (tabletLeaderboard == null)
            {
                return HttpNotFound();
            }
            return View(tabletLeaderboard);
        }

        // POST: TabletLeaderboards/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            TabletLeaderboard tabletLeaderboard = db.TabletLeaderboards.Find(id);
            db.TabletLeaderboards.Remove(tabletLeaderboard);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
